package com.gxa.teax.portal.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;
import javax.jms.ConnectionFactory;
/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: Mr.Wang
 * @create: 2022-07-12 09:47
 **/
@Configuration
public class ActiveMqConfig {
    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;
    @Value("${spring.activemq.user}")
    private String username;
    @Value("${spring.activemq.password}")
    private String password;
    @Bean
    public ConnectionFactory connectionFactory(){
        return new ActiveMQConnectionFactory(username, password,
                brokerUrl);
    }
    @Bean(name = "jmsMeTemplate")
    public JmsMessagingTemplate jmsMessageTemplate(){
        return new JmsMessagingTemplate(connectionFactory());
    }
    @Bean
    public SimpleJmsListenerContainerFactory containerFactory(ConnectionFactory connectionFactory){
        SimpleJmsListenerContainerFactory containerFactory = new SimpleJmsListenerContainerFactory();
        //关闭事务
        containerFactory.setSessionTransacted(false);
        //开启手动签收,单条签收模式
        containerFactory.setSessionAcknowledgeMode(4);
        containerFactory.setConnectionFactory(connectionFactory);
        return containerFactory;
    }
}
